package com.example.helloandroid;


import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.CompoundButton;
import android.widget.Switch;

public class SwitchTest extends Activity {
    //private static final String TAG = "SwitchTest";

    private Switch mAutoSyncSwitch;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mAutoSyncSwitch = new Switch(this);
        mAutoSyncSwitch.setPadding(0, 0, 16, 0);
        mAutoSyncSwitch.setChecked(ContentResolver.getMasterSyncAutomatically());
        mAutoSyncSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ContentResolver.setMasterSyncAutomatically(isChecked);
                onSyncStateUpdated();
            }
        });

        getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                ActionBar.DISPLAY_SHOW_CUSTOM);
        getActionBar().setCustomView(mAutoSyncSwitch, new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER_VERTICAL | Gravity.RIGHT));
    }

    protected void onSyncStateUpdated() {
        if (mAutoSyncSwitch != null) {
            mAutoSyncSwitch.setChecked(ContentResolver.getMasterSyncAutomatically());
        }
        //mAutoSyncSwitch.setClickable(false);
    }
}
